package com.qianshu.dsjjys.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.qianshu.dsjjys.service.UserService;
import com.qianshu.dsjjys.utils.uuidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class UserServiceImpl implements UserService {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public String add(JSONArray json) {
        Map<String, Object> lists = new HashMap();
        String uuid = uuidUtil.getuuid();
        for (int i = 0; i < json.size(); i++) {
            String name = json.getJSONObject(i).getString("name");
            String idCard = json.getJSONObject(i).getString("idCard");
            String sql = "insert into data_contrast(Name,IdCard,Version) value(?,?,?)";
            jdbcTemplate.update(sql, name, idCard, uuid);
            lists.put("flowNo", uuid);
        }
        return lists.toString();
    }

    public String caller(String projectId, String jobId, String version) throws IOException {
        String[] cmds = new String[5];
        cmds[0] = "sh";
//        cmds[1]="/app/webContent/test/test1.sh";
        cmds[1] = "/jb/done.sh";
        cmds[2] = projectId;
        cmds[3] = jobId;
        cmds[4] = version;
        for (String a : cmds) {
            System.out.println("参数：" + a);
        }
        Process pcs = Runtime.getRuntime().exec(cmds);
        String result = null;
        BufferedInputStream in = new BufferedInputStream(pcs.getInputStream());
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String lineStr;
        while ((lineStr = br.readLine()) != null) {
            result = lineStr;
        }
// 关闭输入流
        br.close();
        in.close();

        System.out.println("==============================" + result);
        Map map = new HashMap();
        map.put("fileName", "idcard" + version + ".cvs");
        return JSON.toJSONString(map);
    }

    public Object downloadFiles(String fileName, HttpServletResponse response) {
        try {
            // path是指欲下载的文件的路径。
//            fileName = "/home/work/data/"+fileName;
            fileName = "D:\\test1\\s1.txt";
            File file = new File(fileName);
            // 取得文件名。
            String filename = file.getName();
//            String file0=file.getAbsolutePath();
            // 取得文件的后缀名。
            String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();
//            String ext = file0.substring(file0.lastIndexOf(".") + 1).toUpperCase();
            // 以流的形式下载文件。
            InputStream fis = new BufferedInputStream(new FileInputStream(fileName));
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            fis.close();
            // 清空response
            response.reset();
            // 设置response的Header
            response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
            response.addHeader("Content-Length", "" + file.length());
            OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
            response.setContentType("application/octet-stream");
            toClient.write(buffer);
            toClient.flush();
            toClient.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        return null;
    }

}
